package com.example.asus.homework11.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.asus.homework11.R;
import com.example.asus.homework11.Url;
import com.example.asus.homework11.bean.ShopBean;
import com.squareup.picasso.Picasso;

import java.util.List;


public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyViewHolder> {

    private List<ShopBean> beans;
    private OnItemClickListener onItemClickListener;
    private Context context;

    public ShopAdapter(Context context){
        this.context=context;
    }

    public interface OnItemClickListener{
        void setItem(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<ShopBean> beans){
        this.beans = beans;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_shop,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int i) {
        ShopBean bean = beans.get(i);
        final int position = i;
        Picasso.get()
                .load(Url.BASEUSRL + bean.getPic())
                .into(myViewHolder.iv_shop);
        myViewHolder.tv_shop_name.setText(bean.getShopname());
        myViewHolder.tv_shop_address.setText(bean.getAddress());
        myViewHolder.tv_shop_level.setRating(bean.getLevel());
        if (onItemClickListener != null) {
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.setItem(v, position);

                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return beans==null?0:beans.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public View itemView;
        private ImageView iv_shop;
        private TextView tv_shop_name,tv_shop_address;
        private RatingBar tv_shop_level;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView=itemView;
            iv_shop=itemView.findViewById(R.id.iv_shop);
            tv_shop_name=itemView.findViewById(R.id.tv_shop_name);
            tv_shop_address=itemView.findViewById(R.id.tv_shop_address);
            tv_shop_level=itemView.findViewById(R.id.tv_shop_level);

        }
    }
}
